package com.kinon.norogamilk.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.TextView;

import com.kinon.norogamilk.R;
import com.kinon.norogamilk.application.Constant;
import com.kinon.norogamilk.bean.SpecInfoBean;
import com.kinon.norogamilk.http.MilkApi;
import com.kinon.norogamilk.http.StrCallback;
import com.kinon.norogamilk.ui.base.BaseActivity;
import com.kinon.norogamilk.util.MyDecimalFormat;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.Locale;

import io.reactivex.disposables.CompositeDisposable;
import retrofit2.Call;
import retrofit2.Response;

/**
 * Created by lhqq on 2017-12-18.
 * 规模选择
 */

public class SpecSelectActivity extends BaseActivity implements View.OnClickListener {

    protected CompositeDisposable mCompositeDisposable;
    private Button btn_back;
    private Button btn_pay;
    private EditText et_memberId;
    private TextView btn_memberLogin;
    private ImageView btn_add;
    private ImageView btn_less;
    private TextView tv_isMember;
    private TextView tv_num;
    private TextView tv_mumberPrice;
    private RadioButton rb_specMax;
    private RadioButton rb_specMin;
    private RadioButton rb_normal;
    private RadioButton rb_heat;
    private int mMilkType = 0;
    private int mGoodId = 0;
    private int mMilkNum = 1;
    private double mMilkPrice = 8.0;
    private boolean mIsHeat = false;
    private boolean mIsChangeBg = true;
    private String mMember = "0";
    private Double mDiscount = 1.0;
    private ArrayList<SpecInfoBean> specinfoList = new ArrayList<>();


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_spec_layout);
        initView();
    }

    private void initView() {
        btn_back = findViewById(R.id.btn_back);
        btn_pay = findViewById(R.id.btn_pay);
        et_memberId = findViewById(R.id.et_memberId);
        btn_memberLogin = findViewById(R.id.btn_memberLogin);
        btn_add = findViewById(R.id.btn_add);
        btn_less = findViewById(R.id.btn_less);
        tv_num = findViewById(R.id.tv_num);
        tv_isMember = findViewById(R.id.tv_isMember);
        tv_mumberPrice = findViewById(R.id.tv_mumberPrice);
        rb_specMax = findViewById(R.id.rb_specMax);
        rb_specMin = findViewById(R.id.rb_specMin);
        rb_normal = findViewById(R.id.rb_normal);
        rb_heat = findViewById(R.id.rb_heat);

        mCompositeDisposable = new CompositeDisposable();
//        mMilkType=getIntent().getIntExtra(Constant.MILK_TYPE,0);
        if ((ArrayList<SpecInfoBean>) getIntent().getSerializableExtra("specs") != null) {
            specinfoList = (ArrayList<SpecInfoBean>) getIntent().getSerializableExtra("specs");
            Log.e("lh", "specinfoList== " + specinfoList.get(0).toString());
        }

        addOnclick();
        setView();
    }

    private void setView() {
        if (specinfoList != null && specinfoList.size() > 0) {
            rb_specMax.setText(specinfoList.get(0).getSpec());
            rb_specMin.setText(specinfoList.get(1).getSpec());
            mMilkPrice = specinfoList.get(0).getPrice();
            mGoodId = specinfoList.get(0).getId();
            tv_num.setText(String.valueOf(mMilkNum));
            tv_mumberPrice.setText(String.valueOf(mMilkPrice));
            rbBg();
        }

    }

    private void rbBg() {
        if (specinfoList.get(mMilkType).getStock() <= 0) {
            rb_specMin.setBackgroundResource(R.drawable.celect_specrighterror_img);
            rb_specMax.setBackgroundResource(R.drawable.celect_speclefterror_img);

        } else {
            rb_specMax.setBackgroundResource(R.drawable.celect_specleft_img);
            rb_specMin.setBackgroundResource(R.drawable.celect_specright_img);
        }
    }

    //会员登录
    private void memberCheck() {
        //15068822903
        String memberId = et_memberId.getText().toString();
        Call<String> call = MilkApi.getInstance().memberCheck(sn, memberId);
        call.enqueue(new StrCallback<String>() {
            @Override
            public void onSuccess(Response<String> response, String data) {
                mToast("会员登录成功");
                tv_isMember.setText("会员价:");

                try {
                    JSONObject object = new JSONObject(data);
                    mMember = object.getString("userid");
                    mDiscount = object.getDouble("discount");
                    setMumberPrice();
//                    tv_mumberPrice.setText(String.valueOf(mMilkNum*mMilkPrice*mDiscount));
                } catch (JSONException e) {
                    e.printStackTrace();
                }

            }

            @Override
            public void onError(int code, String error) {
                mToast(error);
            }
        });
    }

    //保存订单
    private void saveOrder() {

        //15068822903
        btn_pay.setEnabled(false);
        String memberId = et_memberId.getText().toString();
        Call<String> call = MilkApi.getInstance()
                .saveOrder(sn, mGoodId + "", mMilkNum + "", mMember);
        call.enqueue(new StrCallback<String>() {
            @Override
            public void onSuccess(Response<String> response, String data) {
                Intent intent = new Intent(SpecSelectActivity.this, ScanCodePayActivity.class);
                intent.putExtra(Constant.MILK_TYPE, specinfoList.get(0).getCategoryid());
                intent.putExtra("spec", specinfoList.get(mMilkType).getSpec());
                intent.putExtra("milkNum", mMilkNum);
                intent.putExtra("milkPrice", MyDecimalFormat.dFormat(mMilkNum * mMilkPrice * mDiscount));
                intent.putExtra("mIsHeat", mIsHeat);
                intent.putExtra("orderurl", data);
                startActivity(intent);
                btn_pay.setEnabled(true);


            }

            @Override
            public void onError(int code, String error) {
                mToast(error);
                btn_pay.setEnabled(true);
            }
        });
    }

    private void addOnclick() {
        btn_back.setOnClickListener(this);
        btn_pay.setOnClickListener(this);
        btn_memberLogin.setOnClickListener(this);
        btn_less.setOnClickListener(this);
        btn_add.setOnClickListener(this);
        rb_specMax.setOnClickListener(this);
        rb_specMin.setOnClickListener(this);
        rb_normal.setOnClickListener(this);
        rb_heat.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.btn_back:
                finish();
                break;
            case R.id.btn_pay:
                saveOrder();
                break;
            case R.id.btn_memberLogin:
                memberCheck();
                break;
            case R.id.btn_add:
                countAdd(true);
                break;
            case R.id.btn_less:
                countAdd(false);
                break;
            case R.id.rb_specMax:
                mMilkType = 0;
                rbBg();
                mGoodId = specinfoList.get(0).getId();
                mMilkPrice = specinfoList.get(0).getPrice();
                setMumberPrice();
//                tv_mumberPrice.setText(String.valueOf(mMilkNum*mMilkPrice*mDiscount));
                break;
            case R.id.rb_specMin:
                mMilkType = 1;
                rbBg();
                mGoodId = specinfoList.get(1).getId();
                mMilkPrice = specinfoList.get(1).getPrice();

//                tv_mumberPrice.setText(String.valueOf(mMilkNum*mMilkPrice*mDiscount));
                setMumberPrice();
                break;
            case R.id.rb_normal:
                mIsHeat = false;
                break;
            case R.id.rb_heat:
                mIsHeat = true;
                break;
            default:
                break;
        }
    }

    private void countAdd(boolean countType) {
        if (countType) {
            if (mMilkNum + 1 <= specinfoList.get(mMilkType).getStock()) {
                mMilkNum++;
            } else {
                mToast("库存不足");
            }
        } else {
            if (mMilkNum <= 1) {
                return;
            }
            mMilkNum--;
        }
        tv_num.setText(String.format(Locale.getDefault(), "%d", mMilkNum));
        setMumberPrice();
    }

    private void setMumberPrice() {
//        tv_mumberPrice.setText(mMilkNum*mMilkPrice*mDiscount+"");
        tv_mumberPrice.setText(MyDecimalFormat.dFormat(mMilkNum * mMilkPrice * mDiscount));
    }
}
